package com.huifaapp.mall.view;

import android.content.Context;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huifaapp.mall.R;
import com.huifaapp.mall.bean.Tmall_shopkeeperGoodsBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by duanyu on 2017/12/25.
 */

public class CustomViewpagertwo extends RelativeLayout {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };
    private ViewPager customtwof_vp;
    private LinearLayout customtwo_ll_out;
    private View across_data;
    private List<Tmall_shopkeeperGoodsBean.DataBean.ListBean> imgList;
    private MyViewPagerAdapter adapter;

    public CustomViewpagertwo(Context context) {
        super(context);
    }

    public CustomViewpagertwo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomViewpagertwo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private void init() {
        View view = inflate(getContext(), R.layout.customtwo_viewpager, null);
        //添加布局
        addView(view);
        customtwof_vp = (ViewPager) view.findViewById(R.id.customtwo_vp);
        customtwo_ll_out = (LinearLayout) view.findViewById(R.id.customtwo_ll_out);

    }
    private void initPoint() {
        for (int i = 0; i < imgList.size(); i++) {
            View view = new View(getContext());
            view.setBackgroundResource(R.drawable.point_bg);
            //动态生成小圆点
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
            params.rightMargin = 20;
            customtwo_ll_out.addView(view, params);
        }
        //给第一个选中
        View view = customtwo_ll_out.getChildAt(0);
        view.setEnabled(true);
    }
    private void initVp() {
        adapter = new MyViewPagerAdapter();
        customtwof_vp.setAdapter(adapter);

        customtwof_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int count = customtwo_ll_out.getChildCount();
                for (int i = 0; i < count; i++) {
                    View view = customtwo_ll_out.getChildAt(i);
                    //设置小圆点属性
                    view.setEnabled(i == (position %= imgList.size()) ? false : true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    public void setsData(List<Tmall_shopkeeperGoodsBean.DataBean.ListBean> imgList) {
        this.imgList = imgList;
        init();
        initVp();
        initPoint();
    }
    public void startSwith(){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                int currentItem = customtwof_vp.getCurrentItem();
                currentItem++;
                customtwof_vp.setCurrentItem(currentItem);
                handler.postDelayed(this,5000);
            }
        },5000);
    }
    public void stopSwith(){
        handler.removeCallbacksAndMessages(null);
    }
    private class MyViewPagerAdapter extends PagerAdapter {

        private ImageView across_data_image1;
        private ImageView across_data_image2;
        private ImageView across_data_image3;
        private View show_view;
        private TextView across_data_goodsName;
        private TextView across_data_price;
        private TextView across_data_conPrice;
        private TextView across_data_couponMoney;
        private TextView across_data_totalSales;
        private TextView across_data_goodsName2;
        private TextView across_data_price2;
        private TextView across_data_conPrice2;
        private TextView across_data_couponMoney2;
        private TextView across_data_totalSales2;
        private TextView across_data_goodsName3;
        private TextView across_data_price3;
        private TextView across_data_conPrice3;
        private TextView across_data_couponMoney3;
        private TextView across_data_totalSales3;

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            //int i = position % imgList.size();
            across_data = View.inflate(getContext(), R.layout.across_data,null);
            deleteStyle();
            across_data_image1 = (ImageView) across_data.findViewById(R.id.across_data_image1);
            across_data_image2 = (ImageView) across_data.findViewById(R.id.across_data_image2);
            across_data_image3 = (ImageView) across_data.findViewById(R.id.across_data_image3);
            across_data_image1.setScaleType(ImageView.ScaleType.FIT_XY);
            across_data_image2.setScaleType(ImageView.ScaleType.FIT_XY);
            across_data_image3.setScaleType(ImageView.ScaleType.FIT_XY);
            across_data_goodsName = (TextView) across_data.findViewById(R.id.across_data_goodsName);
            across_data_price = (TextView) across_data.findViewById(R.id.across_data_price);
            across_data_conPrice = (TextView) across_data.findViewById(R.id.across_data_conPrice);
            across_data_couponMoney = (TextView) across_data.findViewById(R.id.across_data_couponMoney);
            across_data_totalSales = (TextView) across_data.findViewById(R.id.across_data_totalSales);

            across_data_goodsName2 = (TextView) across_data.findViewById(R.id.  across_data_goodsName2);
            across_data_price2 = (TextView) across_data.findViewById(R.id.across_data_price2);
            across_data_conPrice2 = (TextView) across_data.findViewById(R.id.across_data_conPrice2);
            across_data_couponMoney2 = (TextView) across_data.findViewById(R.id.across_data_couponMoney2);
            across_data_totalSales2 = (TextView) across_data.findViewById(R.id.across_data_totalSales2);

            across_data_goodsName3 = (TextView) across_data.findViewById(R.id.across_data_goodsName3);
            across_data_price3 = (TextView) across_data.findViewById(R.id.across_data_price3);
            across_data_conPrice3 = (TextView) across_data.findViewById(R.id.across_data_conPrice3);
            across_data_couponMoney3 = (TextView) across_data.findViewById(R.id.across_data_couponMoney3);
            across_data_totalSales3 = (TextView) across_data.findViewById(R.id.across_data_totalSales3);
            switch (position){
                case 0:
                    Picasso.with(getContext()).load(imgList.get(position).getMainImageUrl()).into(across_data_image1);
                         across_data_goodsName.setText("\t\t\t\t\t"+imgList.get(position).getGoodsName());
                         across_data_price.setText(imgList.get(position).getPrice()+"");
                         across_data_conPrice.setText(imgList.get(position).getConPrice()+"");
                         across_data_couponMoney.setText(imgList.get(position).getCouponMoney()+"");
                         across_data_totalSales.setText(imgList.get(position).getTotalSales()+"");

                         Picasso.with(getContext()).load(imgList.get(position+1).getMainImageUrl()).into(across_data_image2);
                         across_data_goodsName2.setText("\t\t\t\t\t"+imgList.get(position+1).getGoodsName());
                         across_data_price2.setText(imgList.get(position+1).getPrice()+"");
                         across_data_conPrice2.setText(imgList.get(position+1).getConPrice()+"");
                         across_data_couponMoney2.setText(imgList.get(position+1).getCouponMoney()+"");
                         across_data_totalSales2.setText(imgList.get(position+1).getTotalSales()+"");

                    Picasso.with(getContext()).load(imgList.get(position+2).getMainImageUrl()).into(across_data_image3);
                    across_data_goodsName3.setText("\t\t\t\t\t"+imgList.get(position+2).getGoodsName());
                    across_data_price3.setText(imgList.get(position+2).getPrice()+"");
                    across_data_conPrice3.setText(imgList.get(position+2).getConPrice()+"");
                    across_data_couponMoney3.setText(imgList.get(position+2).getCouponMoney()+"");
                    across_data_totalSales3.setText(imgList.get(position+2).getTotalSales()+"");
                    break;
                case 1:
                    Picasso.with(getContext()).load(imgList.get(position+2).getMainImageUrl()).into(across_data_image1);
                    across_data_goodsName.setText("\t\t\t\t\t"+imgList.get(position+2).getGoodsName());
                    across_data_price.setText(imgList.get(position+2).getPrice()+"");
                    across_data_conPrice.setText(imgList.get(position+2).getConPrice()+"");
                    across_data_couponMoney.setText(imgList.get(position+2).getCouponMoney()+"");
                    across_data_totalSales.setText(imgList.get(position+2).getTotalSales()+"");

                    Picasso.with(getContext()).load(imgList.get(position+3).getMainImageUrl()).into(across_data_image2);
                    across_data_goodsName2.setText("\t\t\t\t\t"+imgList.get(position+3).getGoodsName());
                    across_data_price2.setText(imgList.get(position+3).getPrice()+"");
                    across_data_conPrice2.setText(imgList.get(position+3).getConPrice()+"");
                    across_data_couponMoney2.setText(imgList.get(position+3).getCouponMoney()+"");
                    across_data_totalSales2.setText(imgList.get(position+3).getTotalSales()+"");

                    Picasso.with(getContext()).load(imgList.get(position+4).getMainImageUrl()).into(across_data_image3);
                    across_data_goodsName3.setText("\t\t\t\t\t"+imgList.get(position+3).getGoodsName());
                    across_data_price3.setText(imgList.get(position+4).getPrice()+"");
                    across_data_conPrice3.setText(imgList.get(position+4).getConPrice()+"");
                    across_data_couponMoney3.setText(imgList.get(position+4).getCouponMoney()+"");
                    across_data_totalSales3.setText(imgList.get(position+4).getTotalSales()+"");
                    break;
            }


            container.addView(across_data);
            return across_data;
        }
        //删除样式
        public void deleteStyle(){
////            TextView across_data_text1=(TextView)across_data.findViewById(R.id.across_data_text1);
////            TextView across_data_text2=(TextView)across_data.findViewById(R.id.across_data_text2);
////            TextView across_data_text3=(TextView)across_data.findViewById(R.id.across_data_text3);
//            across_data_text1.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG |Paint.ANTI_ALIAS_FLAG);
//            across_data_text2.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG |Paint.ANTI_ALIAS_FLAG);
//            across_data_text3.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG |Paint.ANTI_ALIAS_FLAG);
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //    super.destroyItem(container, position, object);
            container.removeView((View) object);
        }
    }
}
